package com.babyqiao.wallpaper.exceptionhandler;


import com.babyqiao.wallpaper.common.UnifyResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author qinglongzhai
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public UnifyResult error(Exception e) {
        logger.error(ExceptionLogHandler.getMessage(e));
        e.printStackTrace();
        return UnifyResult.error().message("出错了...");
    }


    /**
     * 自定义异常
     *
     * @Author QLZ
     * @Date 2020/12/29
     * * @param null:
     * * @return: null
     */

    @ExceptionHandler(WallpaperException.class)
    @ResponseBody
    public UnifyResult error(WallpaperException e) {
        logger.error(ExceptionLogHandler.getMessage(e));
        e.printStackTrace();
        return UnifyResult.error().code(e.getCode()).message(e.getMsg());
    }


}
